﻿using Core.SimpleTemp.Common;
using Core.SimpleTemp.Entitys;
using Core.SimpleTemp.Repositories.Internal;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Core.Toolkit.ActionResultHelp;

namespace Core.SimpleTemp.Application
{
    /// <summary>
    /// 人员关系处理
    /// </summary>
    [AutoDi()]
    public class WrkerRelationsService
    {
        CoreDBContext _coreDBContext;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="coreDBContext"></param>
        public WrkerRelationsService(CoreDBContext coreDBContext)
        {
            _coreDBContext = coreDBContext;
        }

        #region  根据区域，获取对应的部门和对应的属地负责岗位以及该岗位对应的人，20200113新增，别删掉
        /// <summary>
        /// 根据区域，获取对应的部门和对应的属地负责岗位以及该岗位对应的人
        /// 说明：介于写此方法时，部门区域关系不明朗，所以没增加区域和部门的关系，所以全部查询都是多次连接单条查询，并未联表，之后可以优化。
        /// </summary>
        /// <param name="AreaId">区域id</param>
        public async Task<AjaxJsonModel> GetRegionInfo(Guid? AreaId)
        {
            //区域实体
            var areaEntity = await _coreDBContext.Set<SysArea>().Where(a => a.Id == AreaId).FirstOrDefaultAsync();
            if (areaEntity == null)
            {
                return new AjaxJsonModel()
                {
                    result = "false",
                    message = "请联系管理员配置区域对应的责任岗位及相关信息",
                    data = null
                };
            }
            //部门实体
            var departmentEntity = await _coreDBContext.Set<SysDepartment>().Where(a => a.Id == areaEntity.SysDepartmentID).FirstOrDefaultAsync();
            //岗位实体
            var dutyEntity = await _coreDBContext.Set<SysDuty>().Where(a => a.Id == areaEntity.LeadDutyID).FirstOrDefaultAsync();
            //用户岗位实体
            var userDutyEntity = await _coreDBContext.Set<SysUserDuty>().Where(a => a.SysDutyID == areaEntity.LeadDutyID).FirstOrDefaultAsync();
            if (userDutyEntity == null)
            {
                return new AjaxJsonModel()
                {
                    result = "false",
                    message = "请联系管理员配置区域对应的责任岗位及相关信息",
                    data = null
                };
            }
            //用户实体
            var userEntity = await _coreDBContext.Set<SysUser>().Where(a => a.Id == userDutyEntity.SysUserId).FirstOrDefaultAsync();

            if (departmentEntity != null && dutyEntity != null && userEntity != null)
            {
                return new AjaxJsonModel()
                {
                    result = "true",
                    data = new AreaInfo() { SysDepartment = departmentEntity, SysDuty = dutyEntity, SysUser = userEntity }
                };
            }
            else
            {
                return new AjaxJsonModel()
                {
                    result = "false",
                    message = "请联系管理员配置区域对应的责任岗位及相关信息",
                    data = null
                };
            }





        }
        #endregion




        //public List<Guid> GetHigherUp(Guid userId, string factoryCode = "")
        //{
        //    #region old
        //    ////查询出当前用户职务
        //    //var sysDepartmentDutyIdList = _coreDBContext.Set<SysUserDuty>().Where(ud => ud.IsDeleted == false && ud.SysUserId == userId).Select(ud => ud.SysDepartmentDutyId);// 得到SysDepartmentDutyId
        //    ////查询出职务所在部门
        //    //var departmentIDList = _coreDBContext.Set<SysDepartmentDuty>().Where(model => model.IsDeleted == false && sysDepartmentDutyIdList.Contains(model.Id)).Select(model => model.SysDepartmentID).Distinct();
        //    ////查询出部门领导的SysDepartmentDutyid
        //    //var leaderIdList = _coreDBContext.Set<SysDepartmentDuty>().Where(model => model.IsDeleted == false && departmentIDList.Contains(model.SysDepartmentID) && model.IsLeader == true).Select(model => model.Id);

        //    //var leaderUserIdList = _coreDBContext.Set<SysUserDuty>().Where(model => model.IsDeleted == false && leaderIdList.Contains(model.SysDepartmentDutyId)).Select(ud => ud.SysUserId);

        //    //return leaderUserIdList.ToList(); 
        //    #endregion
        //    //查询出当前用户拥有职务 
        //    var sysDepartmentDutyIdList = _coreDBContext.Set<SysUserDuty>().Where(ud => ud.IsDeleted == false && ud.SysUserId == userId).Select(ud => ud.SysDepartmentDutyId);// 得到SysDepartmentDutyId
        //    //查询出职务所在部门
        //    var departmentIDList = _coreDBContext.Set<SysDepartmentDuty>().Where(model => model.IsDeleted == false && sysDepartmentDutyIdList.Contains(model.Id)).Select(model => model.SysDepartmentID).Distinct();
        //    //查询出该部门下所有职务并且是领导的 id
        //    //var dutyidList = _coreDBContext.Set<SysDepartmentDuty>().Where(model => model.IsDeleted == false && departmentIDList.Contains(model.SysDepartmentID) && model.SysDuty.IsLeader.Value).Select(model => model.Id).Distinct();
        //    ////查询出用户ID
        //    //var leaderUserIdList = _coreDBContext.Set<SysUserDuty>().Where(model => model.IsDeleted == false && dutyidList.Contains(model.SysDepartmentDutyId)).Select(model => model.SysUserId).Distinct();
        //    //return leaderUserIdList.ToList(); 
        //}
        /// <summary>
        /// 根据角色名称获取对应角色下的所有用户id
        /// </summary>
        /// <param name="RoleName"></param>
        /// <returns></returns>
        public List<Guid> GetRoleUser(string RoleName)
        {
            //根据角色名称获取角色id 
            var RoleId = _coreDBContext.Set<SysRole>().Where(x => x.Name == RoleName).Select(y => y.Id).FirstOrDefault();
            //根据角色id获取对应的用户集合
            var UserIdList = _coreDBContext.Set<SysUserRole>().Where(x => x.SysRoleId == RoleId).Select(y => y.SysUserId);

            return UserIdList.ToList();
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public class UserDepartmentDuty
    {
        public Guid UserID { get; set; }
        public string UserName { get; set; }

        public Guid DepartmentId { get; set; }
        public string DepartmentCode { get; set; }
        public string DepartmentName { get; set; }


        public Guid DutyId { get; set; }
        public string DutyCode { get; set; }
        public string DutyName { get; set; }
    }
    /// <summary>
    /// 新增，别删
    /// </summary>
    public class AreaInfo
    {
        public SysDepartment SysDepartment { get; set; }

        public SysUser SysUser { get; set; }

        public SysDuty SysDuty { get; set; }
    }
}
